package com.xiaoj;

public class ThreadState {

    public static void main(String[] args) throws InterruptedException {
        C c = new C();

        B b = new B(c);
        Thread thread1 = new Thread(b);
        thread1.start();

        T t = new T(c);
        Thread thread = new Thread(t);
        System.out.println("start 前线程状态：" + thread.getState());
        thread.start();
        System.out.println("start 后线程状态：" + thread.getState());

        while (Thread.State.TERMINATED != thread.getState()){
            System.out.println("监控线程状态：" + thread.getState());
            Thread.sleep(500);
        }
        System.out.println("最终线程状态：" + thread.getState());
    }
}

class T implements Runnable {

    private C c;

    public T(C c) {
        this.c = c;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("程序员小杰正在跑步");
        }
        System.out.println("程序员小杰跑完步了，肚子疼");
        c.call();
    }
}

class B implements Runnable{
    private C c;

    public B(C c) {
        this.c = c;
    }
    @Override
    public void run() {
        c.call();
    }
}
class C {

    public synchronized void call(){
        System.out.println("上厕所");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}